package com.hxkcloud.nurse.activitys;

import android.os.Bundle;
import android.os.Message;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.adapter.NewsDetailAdapter;
import com.hxkcloud.nurse.beans.resultbeans.BaseResultBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/13.
 */
public class NewsDetailActivity extends BaseActivity implements View.OnClickListener, NewsDetailAdapter.ResultSelectedListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_collect;

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private List<String> newsList;
    private NewsDetailAdapter adapter;

    private AppBarLayout appbar;

    private BaseResultBean resultBean;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_COLLECTARTICALINFO_SUCCESS:
                if (msg.obj != null) {
                    resultBean = (BaseResultBean) msg.obj;
                    toastMsg(resultBean.getMessage());
                    finish();

                }

                break;
            case MessageCode.NETWORKCODE_COLLECTARTICALINFO_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newsdetail_layout);
        newsList = new ArrayList<String>();
        for (int i = 0; i < 3; i++) {
            newsList.add("");
        }
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_collect = (TextView) findViewById(R.id.tv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText(getResources().getText(R.string.newsdetail_text_title));
        tv_collect.setVisibility(View.VISIBLE);
        tv_collect.setText(getResources().getText(R.string.newsdetail_text_collect));
        tv_collect.setOnClickListener(this);

        refreshLayout = (PullToRefreshLayout) findViewById(R.id.newsdetail_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.newsdetail_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);

        adapter = new NewsDetailAdapter(this, newsList, recycleView, this);
        recycleView.setAdapter(adapter);

        appbar = (AppBarLayout) findViewById(R.id.newsdetail_appbar_discover);

        //appbar是否显示 的监听，用来控制列表是否可以刷新
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset >= 0) {
                    setRefreshble(true, 0);

                } else if (verticalOffset <= -appBarLayout.getMeasuredHeight()) {
                    setRefreshble(true, 1);
                } else {
                    setRefreshble(false, 0);
                }
            }
        });


    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.tv_title_func:
                //收藏    0收藏1取消收藏
                YiJiaHuNetWorkUtils.collectArtical("4", "0", "1", handler);
                break;

        }
    }

    public void setRefreshble(boolean isready, int type) {
        if (isready) {
            if (type == 0) {
//                plv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
//                refreshLayout.setCanPull(true, false);
                recycleView.setCanRefresh(true);
                recycleView.setCanLoadMore(false);
            } else {
//                plv.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
//                refreshLayout.setCanPull(false, true);
                recycleView.setCanRefresh(false);
                recycleView.setCanLoadMore(true);
            }
        } else {
//            plv.setMode(PullToRefreshBase.Mode.DISABLED);
//            refreshLayout.setCanPull(false, false);
            recycleView.setCanRefresh(false);
            recycleView.setCanLoadMore(false);
        }

    }

    @Override
    public void onSelected(String type, String info) {

    }
}
